//  我的-录取查询页面

const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    MAXlist: 20, //微信小程序本地查询数据库的限制额
    timer: '', //防抖的定时器
    departmentName: "网络研发部", //传入的部门名
    realName: "", //搜索的用户名,
    realNameregExp: '.*', //构建的名字正则表达式
    phoneNumberregExp: '', //构建电话号的正则表达式
    usersForm: [],
    _openID_fileID_ObjArray: [], // ——objectArray 从云函数获取回来的包括了openid和fileid的对象数组
    openidArray: [] //——Array提取出来的openID数组,

  },
  //自定义函数
  //====================================================
  //点击报名表后跳转详细
  _viewItem(e) {
    console.log(e);
    wx.navigateTo({
      url: '../checkItemapplication/checkItemapplication?openid=' + e.currentTarget.dataset.openid + '&departmentName=' + this.data.departmentName,
    })
  },

  searchApplication(e) {
    //防抖处理
    clearTimeout(this.timer)
    this.timer = setTimeout(() => {
      //先对数组进行清空
      this.setData({
        usersForm: [],
      })
      var inputValue = e.detail.value
      console.log(e.detail.value)
      var realNameregExp = '.*'
      //判断输入的为名字还是电话号
      if (!(/^[0-9]*$/.test(inputValue))) {
        console.log('这是中文');
        //搜索名字不为初始值则构建正则
        if (inputValue != '') {
          for (let i = 0; i < inputValue.length; i++) {
            realNameregExp = realNameregExp + '.*' + inputValue.charAt(i) + "+"
          }
          console.log("构建后的正则" + realNameregExp)
          this.setData({
            realNameregExp: realNameregExp
          })
          // this._queryUserslist();
          this._queryUserslist_2();
        }
      } else {
        console.log('这是数字');
        //如果为电话号则在尾部构建正则表达式
        inputValue = inputValue + '+.*';
        console.log(inputValue);
        this.setData({
          phoneNumberregExp: inputValue
        })
        this._queryUserslist_byphone();
      }

    }, 1500);

  },

  _queryUserslist_byphone() {
    db.collection("Users").where({
        phoneNumber: db.RegExp({
          regexp: this.data.phoneNumberregExp,
          options: 'i'
        }),
        _openid: _.in(this.data.openidArray)
      } //async处理异步
    ).count().then(async res => {
      var total = res.total
      var batchTime = Math.ceil(total / this.data.MAXlist)
      var oldData = [] //用于获取的数据进行暂时储存
      for (let i = 0; i < batchTime; i++) {
        await db.collection("Users").skip(i * this.data.MAXlist).limit(this.data.MAXlist).where({
          phoneNumber: db.RegExp({
            regexp: this.data.phoneNumberregExp,
            options: 'i'
          }),
          _openid: _.in(this.data.openidArray)
        }).get().then(async res => {
          oldData = oldData.concat(res.data)
        })
      }
      //获取数据完成后将数据进行储存
      this.setData({
        usersForm: oldData
      })
      console.log('查询用户表2.0');
      console.log(this.data.usersForm);
    })
  },

  //进化版本
  _querryApplicationID_v2() {
    const that = this
    db.collection('Willings').where({
      willing: _.elemMatch({
        departmentName: that.data.departmentName
      })
    }).count().then(async res => {
      var total = res.total
      var batchTime = Math.ceil(total / that.data.MAXlist)
      var oldData = [] //用于获取的数据进行暂时储存
      for (let i = 0; i < batchTime; i++) {
        await db.collection('Willings').skip(i * that.data.MAXlist).limit(that.data.MAXlist).where({
          willing: _.elemMatch({
            departmentName: that.data.departmentName
          })
        }).get().then(async res => {
          oldData = oldData.concat(res.data)
        })
      }
      //获取完成后将数据储存
      that.setData({
        _openID_fileID_ObjArray: oldData
      })
      console.log('获取ID2.0方法');
      console.log(that.data._openID_fileID_ObjArray);
      //将得到openID对象数组拿出来放进一个单独的数列中
      var openidArray = []
      for (let val of that.data._openID_fileID_ObjArray) {
        openidArray.push(val._openid)
      }
      this.setData({
        openidArray: openidArray
      })
      console.log('2.0版本提取后的openID数组');
      console.log(this.data.openidArray);
    })

  },

  //查询用户表函数——version2
  //搜索条件为：名字为正则表达式，且，_openID位符合条件的_openID数组中
  _queryUserslist_2() {
    //先统计数目
    db.collection("Users").where({
        realName: db.RegExp({
          regexp: this.data.realNameregExp,
          options: 'i'
        }),
        _openid: _.in(this.data.openidArray)
      } //async处理异步
    ).count().then(async res => {
      var total = res.total
      var batchTime = Math.ceil(total / this.data.MAXlist)
      var oldData = [] //用于获取的数据进行暂时储存
      for (let i = 0; i < batchTime; i++) {
        await db.collection("Users").skip(i * this.data.MAXlist).limit(this.data.MAXlist).where({
          realName: db.RegExp({
            regexp: this.data.realNameregExp,
            options: 'i'
          }),
          _openid: _.in(this.data.openidArray)
        }).get().then(async res => {
          oldData = oldData.concat(res.data)
        })
      }
      //获取数据完成后将数据进行储存
      this.setData({
        usersForm: oldData
      })
      console.log('查询用户表2.0');
      console.log(this.data.usersForm);
    })

  },

  // }
  /**
  //====================================================
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    this.setData({
      departmentName: options.departmentName
    })
    // this._querryApplicationID_v2();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})